package com.ykdl.member.views;

import java.util.List;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 辣妈育儿首页 
 * 医生说医生模块视图
 * @author yuanwenfei
 *
 */
public class DoctorTalkchild extends RelativeLayout {
	
	private Context  context;
	private LinearLayout ll_home_experts_forms;
	private FrameLayout fl_experts_forms_item;
	private AsyncImageView   iv_experts_forms;
	private TextView tv_experts_forms_content;
	
	private LinearLayout ll_doctor_question_answer;
	private FrameLayout  fl_doctor_question_answer_item;
	private AsyncImageView iv_doctor_question_answer;
	private TextView  tv_doctor_answer_content;
	
	private List<ArticleBean>  articles = null;  // 专家讲堂(文章)
	private List<CatalogBean>  catalogs = null; // 医生问（答诊所精选）
	private RelativeLayout rl_home_doctor_talk_child_items;
	private int screen_widht;

	private RelativeLayout item_parent_relative_layout;
	

	public DoctorTalkchild(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public DoctorTalkchild(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public DoctorTalkchild(Context context) {
		super(context);
	}
	public void setData(List<ArticleBean>  articles, List<CatalogBean>  catalogs){
		this.articles = articles;
		this.catalogs = catalogs;
		
	}
	public void initView(Context context){
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.doctor_talk_child_view, this, true);
		/**专家讲堂视图**/
//		ll_home_experts_forms = (LinearLayout)findViewById(R.id.ll_home_experts_forms);
		/**医生问答视图**/
//		ll_doctor_question_answer = (LinearLayout)findViewById(R.id.ll_doctor_question_answer);
		/**填充内容的父布局**/
		rl_home_doctor_talk_child_items = (RelativeLayout) findViewById(R.id.rl_home_doctor_talk_child_items);
		
		screen_widht = MobileUtils.getScreenWidthIntPx();
		
		item_parent_relative_layout = new RelativeLayout(context);
		item_parent_relative_layout.setBackgroundColor(getResources().getColor(R.color.test));

		binddata();
		
		
	}

	private void binddata() {
		// TODO Auto-generated method stub
		  ArticleBean article = null;
		  CatalogBean catalog = null;
		if(articles !=null  && catalogs != null){
			article = articles.get(0);
			catalog = catalogs.get(0);
			
			final DoctorTalkChildItem article_item_view = new DoctorTalkChildItem(context, R.drawable.home_doctor_experts);
			final DoctorTalkChildItem catalog_item_view = new DoctorTalkChildItem(context, R.drawable.home_doctor_answer_icon);
			
			bindExpertsFormsView(article, article_item_view);
			bindDoctorQuestionAnswerView(catalog, catalog_item_view);
			
			/**添加专家讲堂视图内容**/
			final LayoutParams article_item_parent_layout_params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			setMargin(article_item_parent_layout_params);
			article_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
			item_parent_relative_layout.addView(article_item_view, article_item_parent_layout_params);
			
			
			/**添加医生问答视图内容**/
			final  LayoutParams catalog_item_parent_layout_params  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			setMargin(catalog_item_parent_layout_params);
			catalog_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
			item_parent_relative_layout.addView(catalog_item_view, catalog_item_parent_layout_params);
			
			rl_home_doctor_talk_child_items.addView(item_parent_relative_layout);
		}else if(articles != null && catalogs == null){
			if(articles.size() >= 2){
				for(int i = 0; i < 2; i++){
					article = articles.get(i);
					final  LayoutParams article_item_parent_layout_params  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
					setMargin(article_item_parent_layout_params);
					final DoctorTalkChildItem article_item_view = new DoctorTalkChildItem(context, R.drawable.home_doctor_experts);
					bindExpertsFormsView(article, article_item_view);
					if(i == 0){
						article_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
						item_parent_relative_layout.addView(article_item_view, article_item_parent_layout_params);
					}else{
						article_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
						item_parent_relative_layout.addView(article_item_view, article_item_parent_layout_params);
					}
				}
				rl_home_doctor_talk_child_items.addView(item_parent_relative_layout);
			}else{
				final  LayoutParams article_item_parent_layout_params  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				final DoctorTalkChildItem article_item_view = new DoctorTalkChildItem(context, R.drawable.home_doctor_experts);
				article = articles.get(0);
				bindExpertsFormsView(article, article_item_view);
				setMargin(article_item_parent_layout_params);
				article_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
				item_parent_relative_layout.addView(article_item_view, article_item_parent_layout_params);
				rl_home_doctor_talk_child_items.addView(item_parent_relative_layout);
				
			}
			
			
		}else if(articles == null && catalogs != null){
			if(catalogs.size() >= 2){
				for(int i = 0; i < 2; i++){
					catalog = catalogs.get(i);
					final  LayoutParams catalog_item_parent_layout_params  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
					setMargin(catalog_item_parent_layout_params);
					final DoctorTalkChildItem catalog_item_view = new DoctorTalkChildItem(context, R.drawable.home_doctor_answer_icon);
					bindDoctorQuestionAnswerView(catalog, catalog_item_view);
					if(i == 0){
						catalog_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
						item_parent_relative_layout.addView(catalog_item_view, catalog_item_parent_layout_params);
					}else{
						catalog_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
						item_parent_relative_layout.addView(catalog_item_view, catalog_item_parent_layout_params);
					}
				}
				rl_home_doctor_talk_child_items.addView(item_parent_relative_layout);
			}else{
				final DoctorTalkChildItem catalog_item_view = new DoctorTalkChildItem(context, R.drawable.home_doctor_answer_icon);
				catalog = catalogs.get(0);
				bindDoctorQuestionAnswerView(catalog, catalog_item_view);
				final  LayoutParams catalog_item_parent_layout_params  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				setMargin(catalog_item_parent_layout_params);
				catalog_item_parent_layout_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
				item_parent_relative_layout.addView(catalog_item_view, catalog_item_parent_layout_params);
				rl_home_doctor_talk_child_items.addView(item_parent_relative_layout);
			}
		}
	}

	private void setMargin(final LayoutParams layout_params) {
		layout_params.leftMargin = MobileUtils.dpToPx(6);
		layout_params.rightMargin = MobileUtils.dpToPx(6);
		item_parent_relative_layout.setLayoutParams(layout_params);
	}



	private void bindExpertsFormsView(ArticleBean article,DoctorTalkChildItem article_item_view) {
		article_item_view.iv_doctor_talk_child.setUrl(((HomeActivity)context).TAG, article.getFile_meta().getDownload_urls().getBig().getUrl(), ImageOptionsFactory.getBigPortraitOptions(context));
		article_item_view.tv_doctor_talk_child.setText(article.getTitle());
	}

	private void bindDoctorQuestionAnswerView(CatalogBean catalog, DoctorTalkChildItem article_item_view) {
		article_item_view.iv_doctor_talk_child.setUrl(((HomeActivity)context).TAG, catalog.getFile_meta().getDownload_urls().getBig().getUrl(),  ImageOptionsFactory.getBigPortraitOptions(context));
		final List<CatalogItemBean> items = catalog.getCatalogItems();
		final CatalogItemBean item = items.get(0);
		if(item instanceof CatalogTopicItem){
			final CatalogTopicItem topicItme = (CatalogTopicItem)item;
			article_item_view.tv_doctor_talk_child.setText(topicItme.getItem().getTitle());
		}else if(item instanceof CatalogArticleItem){
			final CatalogArticleItem articleItem = (CatalogArticleItem)item;
			article_item_view.tv_doctor_talk_child.setText(articleItem.getItem().getContents());
		}
	}

}
